﻿using System;
using System.Collections.Generic;
using System.Linq;
using CTMOES.Models;
using CTMOES.DAL;
using CTMOES;

namespace CTMOES.BLL
{
    /// <summary>
    /// 考试逻辑业务
    /// </summary>
    public class ExamBusiness
    {
        private static readonly CTMDBEntities _db = new CTMDBEntities();
        
        /// <summary>
        /// 获取试卷
        /// </summary>
        /// <param name="paperID">试卷ID</param>
        /// <returns>试卷模型</returns>
        public static PaperModel GetPaper(Guid paperID)
        {
            var tmp = _db.Paper.Where(p => p.ID == paperID).FirstOrDefault();
            PaperModel paper = new PaperModel();

            paper.Choice = QuestionProvider.GetQueList(0,tmp.SubjectID,Convert.ToInt32( tmp.ChoiceNum));
            paper.MultiChoice = QuestionProvider.GetQueList(1, tmp.SubjectID, Convert.ToInt32(tmp.MultiChoiceNum));
            paper.Judgement = QuestionProvider.GetQueList(2, tmp.SubjectID,Convert.ToInt32(tmp.Judgement));


            return paper;
        }

        /// <summary>
        /// 获取总得分
        /// </summary>
        /// <returns></returns>
        public static float GetTotalScore()
        {
            return 0f;
        }

        /// <summary>
        /// 计算单选题得分
        /// </summary>
        /// <param name="choiceAns">单选题答案</param>
        /// <returns>单选题得分</returns>
        public static int CalculateChoice(Dictionary<Guid, Guid> choiceAns, int value)
        {
            int score = 0;
            foreach (var ans in choiceAns)
            {
                if (_db.Options.Where(p => p.ID == ans.Value).FirstOrDefault().Correct == true)
                {
                    score = score + value;
                }
                // ans.Value
            }
            return score;
        }

        /// <summary>
        /// 计算多选题得分
        /// </summary>
        /// <returns>多选题得分</returns>
        public static int CalculateMultiChoice(Dictionary<Guid, bool> MultiChoiceAns, int value)
        {
            int score = 0;
            bool flag, tmp;
            List<Node> mark = new List<Node>();
            foreach (var ans in MultiChoiceAns)
            {
                flag = true;
                tmp = (_db.Options.Where(p => p.ID == ans.Key).FirstOrDefault().Correct == ans.Value);
                foreach (var b in mark)
                {
                    if (b.key == ans.Key)
                    {
                        b.value = (b.value && tmp);
                        flag = true;
                        break;
                    }
                }
                if (flag)
                {
                    mark.Add(new Node(ans.Key, tmp));
                }
            }
            foreach (var node in mark)
            {
                if (node.value)
                {
                    score = score + value;
                }
            }
            return score;
        }

        /// <summary>
        /// 计算判断题得分
        /// </summary>
        /// <returns>判断题得分</returns>
        public static float CalculateJudgement()
        {
            return 0f;
        }

    }
    public class Node
    {
        public Guid key;
        public bool value;
        public Node(Guid Key, bool Value)
        {
            key = Key;
            value = Value;
        }
    }

}